package com.suitsoft.ut.web.imp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.suitsoft.ut.commons.LogAnnotation;
import com.suitsoft.ut.entity.base.StudentTemp;
import com.suitsoft.ut.service.base.StudentTempManager;
import com.suitsoft.ut.util.StringUtils;

/**
 * 确认 学生数据导入
 *@author Suine
 */
public class StudentImpConfirmAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = -7981447265717096372L;

	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	private StudentTempManager studentTempManager;
	
	/** 结果集 */
	private List<Map<String,Object>> resutlist;
	/** 统计 */
	private Map<String,Object> totail;
	/** 学院 */
	private String college;
	/** 学生临时实体集合 */
	private List<StudentTemp> studentList;
	/** 被选的ID */
	private List<Long> checkIds;
	
	@Override
	public String execute() throws Exception {
		resutlist = studentTempManager.getImpList();
		if(resutlist != null && !resutlist.isEmpty()){
			totail = new HashMap<String,Object>();
			int studentCnt = 0;
			int collegeCnt = resutlist.size();
			int departmentCnt = 0;
			int okCnt = 0;
			int noCnt = 0;
			for(Map<String,Object> m : resutlist){
				studentCnt += Integer.valueOf(m.get("C") == null ? "0" : m.get("C").toString());
				departmentCnt += Integer.valueOf(m.get("D") == null ? "0" : m.get("D").toString());
				okCnt += Integer.valueOf(m.get("OK") == null ? "0" : m.get("OK").toString());
				noCnt += Integer.valueOf(m.get("NO") == null ? "0" : m.get("NO").toString());
			}
			totail.put("studentCnt", studentCnt);
			totail.put("collegeCnt", collegeCnt);
			totail.put("departmentCnt", departmentCnt);
			totail.put("okCnt", okCnt);
			totail.put("noCnt", noCnt);
		}
		return SUCCESS;
	}
	
	@Override
	public String input()  {
		try {
			if(StringUtils.isEmpty(college)){
				addActionMessage("非法操作");
				return SUCCESS;
			}
			studentList = studentTempManager.findBy("college", college);
			if(studentList == null ||studentList.isEmpty()){
				addActionMessage("未查到信息！");
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			addActionMessage("操作失败");
			return SUCCESS;
		}
		return INPUT;
	}
	
	@LogAnnotation(log = "基础数据导入 - 学生 - 确认学生数据 - 保存")
	public String save(){
		if(checkIds == null || checkIds.isEmpty()){
			addActionMessage("请选择学生之后，再提交 ");
			return input();
		}
		try {
			studentList = studentTempManager.get(checkIds);
			studentTempManager.saveData(studentList);
			addActionMessage("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			addActionMessage("操作失败");
		}
		return SUCCESS;
	}

	public List<Map<String, Object>> getResutlist() {
		return resutlist;
	}
	public void setResutlist(List<Map<String, Object>> resutlist) {
		this.resutlist = resutlist;
	}
	@Autowired
	public void setStudentTempManager(StudentTempManager studentTempManager) {
		this.studentTempManager = studentTempManager;
	}
	public Map<String, Object> getTotail() {
		return totail;
	}
	public void setTotail(Map<String, Object> totail) {
		this.totail = totail;
	}

	public String getCollege() {
		return college;
	}

	public void setCollege(String college) {
		this.college = college;
	}

	public List<StudentTemp> getStudentList() {
		return studentList;
	}

	public void setStudentList(List<StudentTemp> studentList) {
		this.studentList = studentList;
	}
	public List<Long> getCheckIds() {
		return checkIds;
	}
	public void setCheckIds(List<Long> checkIds) {
		this.checkIds = checkIds;
	}
}

